<template>
    <div class="container">
      <h1 class="title">🍜 智能识别美食图片</h1>
  
      <!-- 上传区域 -->
      <div class="upload-section">
        <input 
          type="file" 
          @change="handleImageUpload" 
          accept="image/*"
          class="upload-input"
          ref="fileInput"
        >
        <div 
          class="upload-area"
          @dragover.prevent
          @drop="handleDrop"
          @click="handleAreaClick"
          :class="{ 'has-image': previewImage }"
        >
          <div v-if="previewImage" class="image-preview">
            <img :src="previewImage" class="preview-image">
            <div class="hover-mask">
              <span class="reupload-text">点击更换图片</span>
            </div>
          </div>
          
          <div v-else class="upload-prompts">
            <img src="" class="upload-icon">
            <p class="upload-tip">点击或拖放图片上传</p>
            <p class="upload-subtip">支持 JPG/PNG 格式</p>
          </div>
        </div>
      </div>
  
      <!-- 加载状态 -->
      <div v-if="isLoading" class="loading">
        <div class="spinner"></div>
        <p class="loading-text">AI正在分析中...</p>
      </div>
  
      <!-- 识别结果 -->
      <div v-if="result && !isLoading" class="result-flow">
        <div class="header-section">
          <h2 class="food-name">{{ result.name }}</h2>
          <div class="rating-box">
            <div class="score-text">{{ result.score }}/10</div>
            <div class="star-rating">
              <div class="star-background">★★★★★</div>
              <div 
                class="star-overlay" 
                :style="{ width: `${(result.score / 10) * 100}%` }"
              >★★★★★</div>
            </div>
          </div>
        </div>
  
        <div class="info-grid">
          <div class="info-item">
            <span class="label">🔥 热量</span>
            <span class="value">{{ result.calories }} kcal</span>
          </div>
          <div class="info-item">
            <span class="label">⏱ 准备时间</span>
            <span class="value">{{ result.prepTime }}</span>
          </div>
          <div class="info-item">
            <span class="label">🌍 地域偏爱</span>
            <span class="value">{{ result.region }}</span>
          </div>
        </div>
  
        <div class="info-block">
          <h3><span class="icon">👩🍳</span> 制作步骤</h3>
          <ol class="step-list">
            <li v-for="(step, index) in processedSteps" :key="index">
              <span class="step-number">步骤 {{ index + 1 }}</span>
              {{ step }}
            </li>
          </ol>
        </div>
  
        <div class="info-block">
          <h3><span class="icon">🥗</span> 原料分析</h3>
          <div class="ingredient-analysis">
            <div 
              v-for="(group, title) in result.ingredients" 
              :key="title"
              class="ingredient-group"
            >
              <h4>{{ title }}</h4>
              <div class="ingredient-tags">
                <span 
                  v-for="(item, idx) in group" 
                  :key="idx"
                  class="tag"
                >{{ item }}</span>
              </div>
            </div>
          </div>
        </div>
  
        <div class="info-block">
          <h3><span class="icon">📜</span> 美食历史</h3>
          <p class="history">{{ result.history }}</p>
        </div>
      </div>
  
      <div v-if="error" class="error">
        ❌ {{ errorMessage }}
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, computed } from 'vue';
  
  const staticData = {
    // ...保持原有测试数据不变...
  };
  
  const previewImage = ref(null);
  const isLoading = ref(false);
  const result = ref(null);
  const error = ref(false);
  const errorMessage = ref('');
  const fileInput = ref(null);
  
  const processedSteps = computed(() => {
    return staticData.cookingMethod
      .split('\n')
      .map(step => step.replace(/^\d+\.\s*/, '').trim())
      .filter(Boolean);
  });
  
  const recognizeFoodAPI = () => {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve({ ...staticData });
      }, 1500);
    });
  };
  
  const handleAreaClick = () => {
    if (!previewImage.value) {
      fileInput.value.click();
    }
  };
  
  const handleImageUpload = async (e) => {
    const file = e.target.files[0];
    if (!file) return;
  
    try {
      isLoading.value = true;
      error.value = false;
      
      const reader = new FileReader();
      reader.onload = (e) => {
        previewImage.value = e.target.result;
      };
      reader.readAsDataURL(file);
  
      result.value = await recognizeFoodAPI();
    } catch (err) {
      error.value = true;
      errorMessage.value = '识别失败，请重试';
    } finally {
      isLoading.value = false;
    }
  };
  
  const handleDrop = (e) => {
    e.preventDefault();
    const file = e.dataTransfer.files[0];
    if (file) handleImageUpload({ target: { files: [file] } });
  };
  </script>
  
  <style scoped>
  .container {
    max-width: 800px;
    margin: 2rem auto;
    padding: 2rem;
    background: #f8f9fa;
    min-height: 100vh;
    font-family: 'Segoe UI', system-ui, sans-serif;
  }
  
  .title {
    font-size: 2.8rem;
    color: #FF6B6B;
    text-align: center;
    margin: 2rem 0;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
  }
  
  /* 上传区域 */
  .upload-section {
    margin: 3rem 0;
  }
  
  .upload-input {
    display: none;
  }
  
  .upload-area {
    position: relative;
    min-height: 320px;
    border: 2px dashed #FFB88C;
    border-radius: 16px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    background: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    cursor: pointer;
    
    &:hover {
      border-color: #FF6B6B;
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(255, 107, 107, 0.2);
    }
  }
  
  .has-image {
    border-style: solid;
    border-color: #FF6B6B;
  }
  
  .image-preview {
    height: 100%;
    border-radius: 14px;
    overflow: hidden;
  }
  
  .preview-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    max-height: 500px;
  }
  
  .hover-mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.4);
    display: none;
    align-items: center;
    justify-content: center;
    color: white;
    backdrop-filter: blur(2px);
  }
  
  .upload-area:hover .hover-mask {
    display: flex;
  }
  
  .upload-prompts {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
  }
  
  .upload-icon {
    width: 64px;
    height: 64px;
    margin-bottom: 1.5rem;
    opacity: 0.8;
  }
  
  .upload-tip {
    font-size: 1.2rem;
    color: #666;
    margin-bottom: 0.5rem;
    font-weight: 500;
  }
  
  .upload-subtip {
    color: #999;
    font-size: 0.9rem;
  }
  
  /* 加载状态 */
  .loading {
    text-align: center;
    padding: 3rem;
    background: white;
    border-radius: 16px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    margin: 2rem 0;
  }
  
  .spinner {
    width: 3rem;
    height: 3rem;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #FF6B6B;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 1rem;
  }
  
  .loading-text {
    color: #666;
    font-size: 1.1rem;
  }
  
  /* 识别结果 */
  .result-flow {
    background: white;
    border-radius: 16px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
    padding: 2.5rem;
    margin: 2rem 0;
  }
  
  .header-section {
    text-align: center;
    margin-bottom: 2.5rem;
  }
  
  .food-name {
    font-size: 2.2rem;
    color: #333;
    margin-bottom: 1.2rem;
    position: relative;
    display: inline-block;
    padding-bottom: 0.5rem;
    
    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      width: 60px;
      height: 3px;
      background: #FF6B6B;
    }
  }
  
  .rating-box {
    display: inline-flex;
    align-items: center;
    gap: 1.2rem;
    background: linear-gradient(135deg, #FFF5F5, #FFE8E8);
    padding: 1rem 2rem;
    border-radius: 24px;
  }
  
  .score-text {
    font-size: 1.6rem;
    font-weight: 700;
    color: #FF6B6B;
  }
  
  .star-rating {
    position: relative;
    font-size: 1.6rem;
  }
  
  .star-background {
    color: #e0e0e0;
  }
  
  .star-overlay {
    position: absolute;
    top: 0;
    left: 0;
    color: #FFD700;
    overflow: hidden;
    white-space: nowrap;
  }
  
  /* 信息网格 */
  .info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    margin: 2.5rem 0;
  }
  
  .info-item {
    background: white;
    padding: 1.5rem;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
    transition: transform 0.2s ease;
    
    &:hover {
      transform: translateY(-3px);
    }
  }
  
  .label {
    color: #FF6B6B;
    font-size: 1rem;
    font-weight: 600;
    display: block;
    margin-bottom: 0.8rem;
  }
  
  .value {
    color: #444;
    font-size: 1.2rem;
    font-weight: 500;
  }
  
  /* 信息区块 */
  .info-block {
    background: white;
    border-radius: 12px;
    padding: 2rem;
    margin: 2rem 0;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.06);
    transition: box-shadow 0.3s ease;
    
    &:hover {
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
    }
    
    h3 {
      color: #2c3e50;
      font-size: 1.4rem;
      margin-bottom: 1.5rem;
      padding-bottom: 0.8rem;
      border-bottom: 2px solid #FFB88C;
      display: flex;
      align-items: center;
      gap: 0.8rem;
    }
  }
  
  .step-list {
    padding-left: 1rem;
  }
  
  .step-list li {
    background: #f8f9fa;
    padding: 1.2rem 1.8rem;
    border-radius: 8px;
    margin: 1.2rem 0;
    position: relative;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
    line-height: 1.6;
  }
  
  .step-number {
    position: absolute;
    left: -1.2rem;
    top: 50%;
    transform: translateY(-50%);
    background: #FF6B6B;
    color: white;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    font-weight: 600;
  }
  
  .ingredient-analysis {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
  }
  
  .ingredient-group h4 {
    color: #666;
    margin-bottom: 0.8rem;
    font-size: 1.1rem;
  }
  
  .ingredient-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.8rem;
  }
  
  .tag {
    background: linear-gradient(145deg, #FFE8E8, #FFD4D4);
    color: #FF6B6B;
    padding: 0.4rem 1rem;
    border-radius: 20px;
    font-size: 0.95rem;
    font-weight: 500;
    border: 1px solid rgba(255, 107, 107, 0.15);
    transition: all 0.2s ease;
    
    &:hover {
      transform: scale(1.05);
      box-shadow: 0 2px 4px rgba(255, 107, 107, 0.2);
    }
  }
  
  .history {
    background: #f8f9fa;
    padding: 1.5rem;
    border-radius: 8px;
    border-left: 4px solid #FF6B6B;
    line-height: 1.8;
    color: #555;
  }
  
  .error {
    color: #ff4444;
    background: #ffeeee;
    padding: 1.2rem;
    border-radius: 8px;
    text-align: center;
    margin: 2rem 0;
    box-shadow: 0 2px 4px rgba(255, 68, 68, 0.1);
  }
  
  @keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
  }
  </style>